프로그래머스 카카오 블라인드 신규 아이디 추천 문제

프로그래머스 카카오 블라인드 신규 아이디 추천 문제

const newId = new_id => {
  const result = removeLastWord(doubleWord(removeWord(lowerCase(new_id))))
    .replace(/^$/g, 'a')
    .substr(0, 15)
    .replace(/\.$/g, '')
    .replace(/^(.)$/, '$1$1$1')
    .replace(/^(.)(.)$/, '$1$2$2')

  return result
}
const lowerCase = id => {
  return id
    .split()
    .map(i => i.toLowerCase())
    .join()
}

const removeWord = id => {
  id = id.match(/[a-z0-9-_.]/g).join('')
  return id
}

const doubleWord = id => {
  const result = id
    .split('')
    .filter((value, index) => value !== id.split('')[index + 1])
    .join('')
  return id.replace(/\.+/g, '.')
}

const removeLastWord = id => {
  let result = id.split('')
  if (result[0] == '.') {
    result.shift()
  }

  if (result[result.length - 1] == '.') {
    result.pop()
  }
  return result.join('')
}

test('lowerCase', () => {
  expect(lowerCase('...!@BaT#*..y.abcdefghijklm')).toBe(
    '...!@bat#*..y.abcdefghijklm'
  )
})

test('removeWord', () => {
  expect(removeWord('...!@bat#*..y.abcdefghijklm')).toBe(
    '...bat..y.abcdefghijklm'
  )
})

test('doubleWord', () => {
  expect(doubleWord('...bat..y.abcdefghijklm')).toBe('.bat.y.abcdefghijklm')
})

test('removeLastWord', () => {
  expect(removeLastWord('.bat.y.abcdefghijklm')).toBe('bat.y.abcdefghijklm')
})

test('newId', () => {
  expect(newId('...!@BaT#*..y.abcdefghijklm')).toBe('bat.y.abcdefghi')
})